import axios from 'axios'
import qs from 'qs'
import { context, isMock } from '@/utils/config'

axios.defaults.headers.common['Content-Type'] = 'application/x-www-form-urlencoded' // 设置成表单提交, 自动适应到post
// axios.interceptors.request.use(config => {
//   let url = config.url.replace(context, '')
//   if(url === '/member/') {
//     return config
//   }
//   let token = localStorage.getItem('token')
//   if(token) {
//     config.headers.token = token  // 请求头中带上token
//   }
//   return config
// }, error => {
//   return Promise.reject(error)
// })

/**
 * 响应直接处理函数
 */
const handleDirect = (res, resolve, reject) => {
  if(isMock) {
    let url = res.config.url
    url = url.replace(context, '')
    console.log(url, res.data)
  }
  if(res.data.errno === undefined || res.data.errno) {
    reject(res.data)
    return
  }
  resolve(res.data)
}

/****************************************
 * 用户
 ****************************************/

/**
 * 用户登录
 * @param  {[String]} username 用户名
 * @param  {[String]} password 密码
 */
export const login = (username, password) => {
  return new Promise((resolve, reject) => {
    axios.post(context + '/member/login', qs.stringify({username, password})).then( res => {
      handleDirect(res, resolve, reject)
    }).catch( error => {
      reject({message: '网络异常, 请稍候再尝试!'})
    })
  })
}

/**
 * 用户认证
 * 防止用户在未登录状况或token无效下访问需授权页面
 */
export const userCertificate = () => {
  return new Promise((resolve, reject) => {
    let token = localStorage.getItem('token')
    if(!token) {
      reject({message: '请先登录!'})
      return
    }
    axios.post(context + '/member/userCertificate', qs.stringify({userLoginToken: token})).then( res => {
      handleDirect(res, resolve, reject)
    }).catch( error => {
      reject({message: '请求异常, 请稍候再试'})
    })
  })
}

/**
 * 获取用户信息
 * @param  {[String]} token 授权码
 */

export const getUser = (token, login) => {
  return new Promise((resolve, reject) => {
    if (!login) {
      axios.get(context + '/member/', {params: {userLoginToken: token}}).then( res => {
        handleDirect(res, resolve, reject)
      }).catch( error => {
        reject({message: '请求异常, 请稍候再试'})
      })
    }else {
      resolve(null)
    }
  })
}

/**
 * 用户退出
 * @param  {[String]} token 授权码
 */
export const loginOut = token => {
  return new Promise((resolve, reject) => {
    axios.delete(context + '/member/?' + qs.stringify({userLoginToken: token})).then( res => {
      resolve(null)
    }).catch( error => {
      reject({message: '请求异常, 请稍候再试'})
    })
  })
}

/**
 * 用户注册
 * @param  {[String]} token    短信验证后获得的授权码
 * @param  {[String]} password 密码
 */
export const register = (token, password) => {
  return new Promise((resolve, reject) => {
    axios.post(context + '/member/register', qs.stringify({userEnhancedAuthorityToken: token, password})).then( res => {
      handleDirect(res, resolve, reject)
    }).catch( error => {
      reject({message: '请求异常, 请稍候再试'})
    })
  })
}

/**
 * 用户密码找回
 * @param  {[String]} token    短信验证后获得的授权码
 * @param  {[String]} password 新密码
 */
export const forget = (token, password) => {
  return new Promise((resolve, reject) => {
    axios.post(context + '/member/changPassword', qs.stringify({userEnhancedAuthorityToken: token, password})).then( res => {
      handleDirect(res, resolve, reject)
    }).catch( error => {
      reject({message: '请求异常, 请稍候再试'})
    })
  })
}

/**
 * 请求短信
 * @param  {[String]} phone 手机号
 * @param  {[Integer]} type  短信类型
 */
export const sms = (phone, type) => {
  return new Promise((resolve, reject) => {
    axios.post(context + '/common/sms', qs.stringify({phone: phone, actionCode: type})).then( res => {
      handleDirect(res, resolve, reject)
    }).catch( error => {
      reject({message: '请求异常, 请稍候再试'})
    })
  })
}

/**
 * 验证短信
 * @param  {[String]} phone 手机号
 * @param  {[Integer]} code  短信验证码
 * @param  {[Integer]} type  短信类型
 */
export const smsCertificate = (phone, code, type) => {
  return new Promise((resolve, reject) => {
    axios.post(context + '/common/checkVerificationCode', qs.stringify({username: phone, verificationCode: code, actionCode: type})).then( res => {
      handleDirect(res, resolve, reject)
    }).catch( error => {
      reject({message: '请求异常, 请稍候再试'})
    })
  })
}

/****************************************
 * 首页
 ****************************************/
export const getHouseList = (city, currentPage) => {
  return new Promise((resolve, reject) => {
    axios.get(context + '/info/homelist?' + qs.stringify({city, currentPage})).then(res => {
      handleDirect(res, resolve, reject)
    }).catch(error => {
      reject({message: '请求异常, 请稍候再试'})
    })
  })
}
